package nio.file;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * FileCopyDemo
 *
 * @author chenxin
 * @date 2022/05/16 17:54
 **/
public class FileCopyDemo {

    public static void main(String[] args) throws Exception {
        String source = "E:\\md笔记";
        String target = "E:\\md笔记222";

        Files.walk(Paths.get(source)).forEach(path -> {
            // 将文件的文件夹部分替换
            String targetName = path.toString().replace(source,target);

            // 如果是目录 创建一个相同的目录
            if(Files.isDirectory(path)){
                try {
                    Files.createDirectory(Paths.get(targetName));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            // 如果是文件 复制一份
            else if(Files.isRegularFile(path)){
                try {
                    Files.copy(path,Paths.get(targetName));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }

}